package com.ihd.ihardware.view.tzc.me.model;

import android.os.Bundle;

import com.google.gson.JsonObject;
import com.ihd.ihardware.api.HttpClient;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.RecommendRes;

import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class FansRepository extends BaseRepository {

    public void concern(String concernUserId) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("concernUserId", concernUserId);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().concern(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("key", emptyRes.getCode());
                        b.putInt("type", 1);
                        b.putString("value", emptyRes.getCode() == 0 ? concernUserId : emptyRes.getMessage());
                        RxBus.getInstance().post(AppConstans.FCONCERN, b);
                    }
                }));
    }

    public void delConcern(String concernUserId) {
        addDisposable(HttpClient.getService().delConcern(concernUserId)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("key", emptyRes.getCode());
                        b.putInt("type", 0);
                        b.putString("value", emptyRes.getCode() == 0 ? concernUserId : emptyRes.getMessage());
                        RxBus.getInstance().post(AppConstans.FCONCERN, b);
                    }
                }));
    }

    public void getFansList() {
        addDisposable(HttpClient.getService().fans()
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<RecommendRes>(AppConstans.FANS) {
                }));
    }
}
